﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Data.SqlClient;
using jiaju.Areas.Back.Models.DBModel;
using jiaju.Areas.Back.Models.DAL;
using jiaju.Areas.Back.Models;

namespace jiaju.Areas.Back.Controllers
{
    [Authorize]
    public class NewsTypeController : Controller
    {
        // GET: Back/NewsType
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult getNewsType(int page, int limit, string name)
        {
            //调用Service里的方法
            List<NewsType> dataList = NewsTypeService.getNewsTypeList(page, limit, name);
            //获取总条数
            string sql = "select count(*) from dbo.newsType";
            if (!string.IsNullOrEmpty(name))
            {
               sql = sql + string.Format(" where Name like '%{0}%'", name);
            }
            int cout = (int)DBHelp.ExcuteScalar(sql);
            object obj = new
            {
                code = 0,
                msg = "请求成功",
                count = cout,
                data = dataList,
            }; 
            return Json(obj, JsonRequestBehavior.AllowGet);
        }
        //添加新闻分类页面   
        public ActionResult Add()
        {
            return View();
        }
        public ActionResult AddDel(NewsType art)
        {
            //新增 请求数据库
            int n = NewsTypeService.AddNewsType(art);
            if (n > 0)
            {
                return Json(new { success = true, msg = "新增成功" });
            }
            else
            {
                return Json(new { success = false, msg = "新增失败" });
            }
        }

        //编辑新闻分类
        public ActionResult Edit(string Id)
        {
            //通过Id获取数据详情
            NewsType art = NewsTypeService.GetById(Id);
            //把详情数据传到页面
            ViewBag.A = art;
            return View();
        }
        [HttpPost]
        public ActionResult EditDel(NewsType art)
        {
            //编辑，请求数据库
            int m = NewsTypeService.EditProductType(art);
            if (m > 0)
            {
                return Json(new { success = true, msg = "编辑成功" });
            }
            else
            {
                return Json(new { success = false, msg = "编辑失败" });
            }
        }
        //删除商品分类方法DelArt
        public ActionResult DelArt(String Id)
        {
            int m = NewsTypeService.DelNewsType(Id);
            if (m > 0)
            {
                return Json(new { success = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { success = false, msg = "删除失败" });
            }
        }

    }
}